 

 
use "${Data_Clean}/panel_regdata.dta", clear
format cbg %15.0g
estimates clear
drop _merge

/*Rename long variable names, create "rename list" for esttab and create full list of gas price variables for sensitivity tests*/
local ranges "lt1 lt3 lt5 lt10"

local renamelist ""
local fullgasprice_vars "P_gas"

foreach x in `ranges' {
	rename  obswgtprice_`x' P_ow_`x' 
	local fullgasprice_vars "`fullgasprice_vars' P_ow_`x'"
	local renamelist "`renamelist' P_ow_`x' P_gas"
}
foreach x in `ranges' {
	rename   invdistwgtprice_`x' P_idw_`x'
	local fullgasprice_vars "`fullgasprice_vars'  P_idw_`x'"
	local renamelist "`renamelist' P_idw_`x' P_gas"
}



merge m:1 year cbg using "${Data_Clean}/panel_demog.dta"
drop _merge

/*Panel Regressions, all CBGs*/
*local gasprice "P_gas"
local gasprice "P_ow_lt3"
local elecprice "p_e_active"


/*How much electricity and gasoline price variation are captured by fixed effects*/

reghdfe `gasprice', absorb(i.modate i.cbg) vce(cluster cbg)
reghdfe `elecprice', absorb(i.modate i.cbg) vce(cluster cbg)



/*Regression Analysis*/
estimates clear

local demog "b01002_001e white_frac BA_fraction b19013_001e"
local other_cov "fuel_economy_mean hybrid_share luxury_share mud_hh_share"
local icempg = 30
local BEVmpkwh = 4

local altgasprice invdistwgtprice



/*Monthly Regressions*/

reghdfe share_BEV `elecprice' `gasprice', noabsorb vce(cluster cbg)
estimates store panel_monthly_noFE

reghdfe share_BEV `elecprice' `gasprice', absorb(i.modate i.cbg) vce(cluster cbg)
estimates store panel_monthly_FEonly
estadd local CBG_FE "X" , replace
estadd local Time_FE "X" , replace
nlcom -(_b[p_e_active]*`BEVmpkwh')/(_b[P_ow_lt3]*`icempg')
estadd local impliedgamma=round(r(b)[1,1],.001) , replace
local gammase=round(sqrt(r(V)[1,1]),.001)
estadd local gammase="("+"`gammase'"+")", replace

reghdfe share_BEV `elecprice' `gasprice' `demog' `other_cov', absorb(i.modate i.cbg) vce(cluster cbg)
estimates store panel_monthly
estadd local CBG_FE "X" , replace
estadd local Time_FE "X" , replace
estadd local Demographics "X" , replace
nlcom -(_b[p_e_active]*`BEVmpkwh')/(_b[P_ow_lt3]*`icempg')
estadd local impliedgamma=round(r(b)[1,1],.001) , replace
local gammase=round(sqrt(r(V)[1,1]),.001)
estadd local gammase="("+"`gammase'"+")", replace


/*Main spec with alternative gas prices*/

local fullmonthresults ""

foreach x of varlist `fullgasprice_vars' {
reghdfe share_BEV `elecprice' `x' `demog' `other_cov', absorb(i.modate i.cbg) vce(cluster cbg)
estadd local Demog "X", replace
estadd local CBG_FE "X" , replace
estadd local Time_FE "X" , replace
nlcom -(_b[`elecprice']*`BEVmpkwh')/(_b[`x']*`icempg')
estadd local impliedgamma=round(r(b)[1,1],.001) , replace
local gammase=round(sqrt(r(V)[1,1]),.001)
estadd local gammase="("+"`gammase'"+")", replace
estimates store panel_monthly_`x'
local fullmonthresults " `fullmonthresults' panel_monthly_`x' "
}

/*Monthly regressions PHEV*/
reghdfe share_PHEV `elecprice' `gasprice', noabsorb vce(cluster cbg)
estimates store panel_monthly_noFE_PHEV

reghdfe share_PHEV `elecprice' `gasprice', absorb(i.modate i.cbg) vce(cluster cbg)
estimates store panel_monthly_FEonly_PHEV
estadd local CBG_FE "X" , replace
estadd local Time_FE "X" , replace
nlcom -(_b[p_e_active]*`BEVmpkwh')/(_b[P_ow_lt3]*`icempg')
estadd local impliedgamma=round(r(b)[1,1],.001) , replace
local gammase=round(sqrt(r(V)[1,1]),.001)
estadd local gammase="("+"`gammase'"+")", replace

reghdfe share_PHEV `elecprice' `gasprice' `demog' `other_cov', absorb(i.modate i.cbg) vce(cluster cbg)
estimates store panel_monthly_PHEV
estadd local CBG_FE "X" , replace
estadd local Time_FE "X" , replace
estadd local Demographics "X" , replace
nlcom -(_b[p_e_active]*`BEVmpkwh')/(_b[P_ow_lt3]*`icempg')
estadd local impliedgamma=round(r(b)[1,1],.001) , replace
local gammase=round(sqrt(r(V)[1,1]),.001)
estadd local gammase="("+"`gammase'"+")", replace



/*Annual Regressions*/

collapse (mean)`demog' `other_cov' eiaid  `elecprice' `fullgasprice_vars' population (sum) Q_BEV Q_PHEV, by(cbg year)

local pop_var "population"
gen share_BEV = Q_BEV/(`pop_var'/10)
gen share_PHEV = Q_PHEV/(`pop_var'/10)

reghdfe share_BEV `elecprice' `gasprice', noabsorb vce(cluster cbg)
estimates store panel_yearly_noFE

reghdfe share_BEV `elecprice' `gasprice', absorb(i.year i.cbg) vce(cluster cbg)
estimates store panel_yearly_FEonly
estadd local CBG_FE "X" , replace
estadd local Time_FE "X" , replace
nlcom -(_b[p_e_active]*`BEVmpkwh')/(_b[P_ow_lt3]*`icempg')
estadd local impliedgamma=round(r(b)[1,1],.001) , replace
local gammase=round(sqrt(r(V)[1,1]),.001)
estadd local gammase="("+"`gammase'"+")", replace

reghdfe share_BEV `elecprice' `gasprice' `demog' `other_cov', absorb(i.year i.cbg) vce(cluster cbg)
estimates store panel_yearly
estadd local CBG_FE "X" , replace
estadd local Time_FE "X" , replace
estadd local Demographics "X" , replace
nlcom -(_b[p_e_active]*`BEVmpkwh')/(_b[P_ow_lt3]*`icempg')
estadd local impliedgamma=round(r(b)[1,1],.001) , replace
local gammase=round(sqrt(r(V)[1,1]),.001)
estadd local gammase="("+"`gammase'"+")", replace



/*Main spec with alternative gas prices*/

local fullyearlyresults ""

foreach x of varlist `fullgasprice_vars' {
reghdfe share_BEV `elecprice' `x' `demog' `other_cov', absorb(i.year i.cbg) vce(cluster cbg)
estadd local Demog "X", replace
estadd local CBG_FE "X" , replace
estadd local Time_FE "X" , replace
nlcom -(_b[`elecprice']*`BEVmpkwh')/(_b[`x']*`icempg')
estadd local impliedgamma=round(r(b)[1,1],.001) , replace
local gammase=round(sqrt(r(V)[1,1]),.001)
estadd local gammase="("+"`gammase'"+")", replace
estimates store panel_yearly_`x'
local fullyearlyresults " `fullyearlyresults' panel_yearly_`x' "
}

/*Annual Regressions PHEV*/

reghdfe share_PHEV `elecprice' `gasprice', noabsorb vce(cluster cbg)
estimates store panel_yearly_noFE_PHEV

reghdfe share_PHEV `elecprice' `gasprice', absorb(i.year i.cbg) vce(cluster cbg)
estimates store panel_yearly_FEonly_PHEV
estadd local CBG_FE "X" , replace
estadd local Time_FE "X" , replace
nlcom -(_b[p_e_active]*`BEVmpkwh')/(_b[P_ow_lt3]*`icempg')
estadd local impliedgamma=round(r(b)[1,1],.001) , replace
local gammase=round(sqrt(r(V)[1,1]),.001)
estadd local gammase="("+"`gammase'"+")", replace

reghdfe share_PHEV `elecprice' `gasprice' `demog' `other_cov', absorb(i.year i.cbg) vce(cluster cbg)
estimates store panel_yearly_PHEV
estadd local CBG_FE "X" , replace
estadd local Time_FE "X" , replace
estadd local Demographics "X" , replace
nlcom -(_b[p_e_active]*`BEVmpkwh')/(_b[P_ow_lt3]*`icempg')
estadd local impliedgamma=round(r(b)[1,1],.001) , replace
local gammase=round(sqrt(r(V)[1,1]),.001)
estadd local gammase="("+"`gammase'"+")", replace





/*Outputting Main Panel Regression Tables*/

local reportvars "`elecprice' `gasprice'"

label var `elecprice' "Marg. Price (cents/kwh)"
label var `gasprice' "Gas Price (cpg)"

esttab panel_monthly_noFE panel_monthly_FEonly panel_monthly panel_yearly_noFE panel_yearly_FEonly panel_yearly  ///
	using "${ResultsOut}/Tables/panel_results.tex", ///
	keep(`reportvars') ///
	replace label se star(* 0.10 ** 0.05 *** 0.01) nogaps nonotes noconstant booktabs ///
	b(a2) se(a2) stats(CBG_FE Time_FE Demographics impliedgamma gammase N r2,  labels("CBG FE" "Time FE" "Demographics" "\\ Implied $\gamma$"   " " "\\ Observations" "R-Squared")) ///
    mgroups("\underline{Monthly Sales Per Cap}" "\underline{Annual Sales Per Cap}", pattern(1 0 0 1 0 0) span prefix(\multicolumn{@span}{c}{) suffix(})) ///
	nomtitles 

	
		
/* Reanalysis with alternative gasoline prices */


local reportvars "`elecprice' P_gas"

label var `elecprice' "Marg. Price (cents/kwh)"
label var P_gas "Gas Price (cpg)"

macro list

esttab `fullmonthresults' ///
	using "${ResultsOut}/Tables/panel_results_monthly_altgas.tex", ///
	keep(`reportvars') ///
	replace label se star(* 0.10 ** 0.05 *** 0.01) nogaps nonotes noconstant booktabs ///
	b(a2) se(a2) stats(Time_FE CBG_FE  impliedgamma gammase N r2,  labels("Time FE" "CBG FE"  "\\ Implied $\gamma$"   " " "\\ Observations" "R-Squared")) ///
	rename(`renamelist') ///
	mtitles("Avg Zip Price" "1mi Radius" "3mi Radius" "5mi Radius" "10mi Radius"  "1mi Radius" "3mi Radius" "5mi Radius" "10mi Radius" ) ///
    mgroups("" "\underline{Observation Weighted Average Prices}" "\underline{Inverse Distance Weighted Average Prices}", pattern(1 1 0 0 0 1 0 0 0) span prefix(\multicolumn{@span}{c}{) suffix(})) 

esttab `fullyearlyresults' ///
	using "${ResultsOut}/Tables/panel_results_yearly_altgas.tex", ///
	keep(`reportvars') ///
	replace label se star(* 0.10 ** 0.05 *** 0.01) nogaps nonotes noconstant booktabs ///
	b(a2) se(a2) stats(Time_FE CBG_FE  impliedgamma gammase N r2,  labels("Time FE" "CBG FE"  "\\ Implied $\gamma$"   " " "\\ Observations" "R-Squared")) ///
	rename(`renamelist') ///
	mtitles("Avg Zip Price" "1mi Radius" "3mi Radius" "5mi Radius" "10mi Radius" "1mi Radius" "3mi Radius" "5mi Radius" "10mi Radius" ) ///
    mgroups("" "\underline{Observation Weighted Average Prices}" "\underline{Inverse Distance Weighted Average Prices}", pattern(1 1 0 0 0 1 0 0 0) span prefix(\multicolumn{@span}{c}{) suffix(})) 



/*Outputting Table for PHEVs*/

local reportvars "`elecprice' `gasprice'"

label var `elecprice' "Marg. Price (cents/kwh)"
label var `gasprice' "Gas Price (cpg)"


esttab panel_monthly_noFE_PHEV panel_monthly_FEonly_PHEV panel_monthly_PHEV panel_yearly_noFE_PHEV panel_yearly_FEonly_PHEV panel_yearly_PHEV  ///
	using "${ResultsOut}/Tables/panel_results_PHEV.tex", ///
	keep(`reportvars') ///
	replace label se star(* 0.10 ** 0.05 *** 0.01) nogaps nonotes noconstant booktabs ///
	b(a2) se(a2) stats(Time_FE CBG_FE  impliedgamma gammase N r2,  labels("Time FE" "CBG FE"  "\\ Implied $\gamma$"   " " "\\ Observations" "R-Squared")) ///
    mgroups("\underline{Monthly Sales Per Cap}" "\underline{Annual Sales Per Cap}", pattern(1 0 1 0) span prefix(\multicolumn{@span}{c}{) suffix(})) ///
	nomtitles 
	
	